/*
 * Copyright 2017-8-30 13:56:26 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.admin.ao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.lap.scd.commons.ao.AbstractAO;
import com.lap.scd.commons.constant.RespCode;
import com.lap.scd.commons.page.Page;
import com.lap.scd.commons.page.PageResult;
import com.lap.scd.commons.page.PageUtils;
import com.lap.scd.commons.result.Resp;
import com.lap.scp.admin.ao.AccountAO;
import com.lap.scp.admin.core.AccountManager;
import com.lap.scp.admin.domain.AccountDO;
import com.lap.scp.admin.query.AccountQueryDO;

/**
 * <pre>
 * 短信账号应用
 * </pre>
 * 
 * @author 系统自动生成 Exp
 * @Date 2017-8-30 13:56:26
 * @since 1.0
 */
@Service("accountAO")
public class DefaultAccountAO extends AbstractAO implements AccountAO {

	@Resource
	private AccountManager accountManager;

	@Override
	public Resp<Integer> deleteById(int accountId) {
		Resp<Integer> resp = new Resp<>();
		try {
			int flag = accountManager.deleteById(accountId);
			resp.setData(flag);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(accountId, e);
		}
		return resp;
	}

	@Override
	public Resp<AccountDO> insertAccount(AccountDO accountDO, BindingResult error) {
		Resp<AccountDO> resp = new Resp<>();
		try {
			super.validate(resp, error, accountDO);
			if (resp.isSuccess()) {
				accountManager.insertAccount(accountDO);
				resp.setData(accountDO);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(accountDO, e);
		}
		return resp;
	}

	@Override
	public Resp<AccountDO> queryById(int accountId) {
		Resp<AccountDO> resp = new Resp<>();
		try {
			AccountDO accountDO = accountManager.queryById(accountId);
			resp.setData(accountDO);
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(accountId, e);
		}
		return resp;
	}

	@Override
	public PageResult<AccountDO> queryPage(Page page, AccountQueryDO accountQueryDO) {
		PageResult<AccountDO> pr = new PageResult<AccountDO>();
		try {
			List<AccountDO> list = accountManager.queryPage(page, accountQueryDO);

			PageUtils.copyPage(pr, page);
			pr.setData(list);
		} catch (Exception e) {
			pr.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(new Object[] { page, accountQueryDO }, e);
		}
		return pr;
	}

	@Override
	public Resp<Integer> updateAccount(AccountDO accountDO, BindingResult error) {
		Resp<Integer> resp = new Resp<>();
		try {
			super.validate(resp, error, accountDO);
			if (resp.isSuccess()) {
				int flag = accountManager.updateAccount(accountDO);
				resp.setData(flag);
			}
		} catch (Exception e) {
			resp.putMsg(RespCode.Error, super.getMsg("505", new String[] { e.getLocalizedMessage() }));
			writeLog(accountDO, e);
		}
		return resp;
	}

}
